<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/6/27
 * Time: 09:37
 */

namespace app\common\model;


use think\Model;

class Group extends Model
{
    //根据用户ID查询数据
    public function getGroupByUserId($id,$page,$pageSize = 10,$status){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }
        //第几页
        $offset = ($page - 1) * $pageSize;
        //查询条件
        $where = array(
            'g.user_id' => $id
        );

        if($status == 1){
            $where['g.status'] = 1;  //拼团成功的状态值
        }else if($status == 2){
            $where['g.status'] = 2;  //拼团中的状态值
        }else if($status == 3){
            $where['g.status'] = 3;  //拼团失败的状态值
        }
        //返回数据给控制器
        return $this->alias("g")->join("xm_group_order go","g.group_order_id=go.id")->join("xm_courses c","go.course_id=c.id")->join("xm_author a","c.author_id=a.id")->where($where)->limit($offset,$pageSize)->field("a.name,c.title,c.g_price,c.img_url,c.price,c.num,c.id as CourseId,c.whenlong,g.id,go.id as groupOrderId,g.status")->select();

    }

    //根据拼团ID查询数据
    public function getGroupOrderById($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }
        $where = array(
            'g.id' => $id,
        );

        return $this->alias("g")->join("xm_group_order go","g.group_order_id=go.id")->join("xm_courses c","go.course_id=c.id")->join("xm_author a","c.author_id=a.id")->join("xm_users u","g.user_id=u.id")->where($where)->field("a.name,c.num,c.id as CourseId,c.title,go.id,c.g_price,c.img_url,u.image_url,u.name as UserName,go.title as OrderTitle,c.price,go.group_number,c.whenlong,go.join_number")->find();

    }
}